/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
    public:
        bool isSubtree(TreeNode* root, TreeNode* subRoot) {
            if(root == nullptr) return false;
            return issame(root,subRoot) || isSubtree(root->left,subRoot) || isSubtree(root->right,subRoot);
        }
        bool issame(TreeNode* p,TreeNode* q){
            if(p == nullptr && q == nullptr) return true;
            if((p == nullptr && q != nullptr) || (p!= nullptr&&q == nullptr)) return false;
            if(p-> val != q->val) return false;
            return issame(p->left,q->left) && issame(p->right,q->right);  
        }
    };
    
    //issubtree任务：遍历树，从跟节点开始遍历树
    //子问题：判断节点开始的树与subtree是否相同
    //注意递归出口